package com.mall.feign.config;

import com.mall.commons.utils.Constants;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Configuration
public class FeignConfig implements RequestInterceptor {
    /**
     * Called for every request. Add data using methods on the supplied {@link RequestTemplate}.
     *
     * @param template
     */
    @Override
    public void apply(RequestTemplate template) {
         ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        获取请求体
        HttpServletRequest request = attributes.getRequest();
//        获取token
        String token = request.getHeader(Constants.TOKEN_AUTHORIZATION);
//        注入feign的请求头
        template.header(Constants.TOKEN_AUTHORIZATION,token);
    }
}

